#! /bin/bash

GO_DOWNLOAD_URL="https://go.dev/dl/go1.18.9.linux-amd64.tar.gz"
GO_INSTALL_ROOT=$HOME/go_install
CURRENT_DIR=$(pwd)
uid=$(id -u)
if [[ $uid -ne 0 ]]
then
    echo "用户权限不足，请使用root用户安装"
fi
if [[ ! -d $GO_INSTALL_ROOT ]]
then
    
    mkdir $GO_INSTALL_ROOT
fi

go version > /dev/null 2>& 1
if [[ $? -ne 0 ]]
then
    which wget >/dev/null
    if [[ $? -ne 0 ]]
    then
        yum -y install wget
    fi

    cd $GO_INSTALL_ROOT
    wget $GO_DOWNLOAD_URL
    if [[ ! -f go1.18.9.linux-amd64.tar.gz ]]
    then
        echo "go安装包下载不成功"
        cd $HOME
        rm -rf $GO_INSTALL_ROOT
        exit 1
    fi
    tar xf go1.18.9.linux-amd64.tar.gz
    cd $CURRENT_DIR
fi
export GOPROXY=https://goproxy.cn,direct
$GO_INSTALL_ROOT/go/bin/go build
if [[ -f msrv ]]
then
    install -m 0755 msrv /usr/bin && echo "安装成功" || echo "安装失败"

else
    echo "安装不成功"
fi 
unset GOPROXY
rm -rf $GO_INSTALL_ROOT